<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="close">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
        <el-input v-model="form.username" clearable autocomplete="off" placeholder="请输入员工姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" clearable autocomplete="off" laceholder="请输入员工手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry" :label-width="formLabelWidth">
        <el-date-picker v-model="form.timeOfEntry" value-format="yyyy-MM-dd" type="date" placeholder="请选择员工入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" :label-width="formLabelWidth">
        <el-select v-model="form.formOfEmployment" clearable placeholder="请选择员工聘用形式">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber" :label-width="formLabelWidth">
        <el-input v-model="form.workNumber" clearable autocomplete="off" placeholder="请输入员工工号" />
      </el-form-item>
      <el-form-item ref="form" label="部门" prop="departmentName" :label-width="formLabelWidth">
        <el-input v-model="form.departmentName" clearable autocomplete="off" placeholder="请选择员工所属部门" @focus="deptFocus" @blur="deptBlur" />
        <el-tree v-show="treeShow" v-loading="treeLoading" :data="treeData" :props="defaultProps" :default-expand-all="true" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime" :label-width="formLabelWidth">
        <el-date-picker v-model="form.correctionTime" value-format="yyyy-MM-dd" type="date" placeholder="请选择员工转正时间" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import enumObj from '@/utils/constant/employees'
import { getDepts } from '@/api/departments'
import { arrayToTree } from '@/utils'
import { addEmp } from '@/api/employees'
export default {
  name: 'AddEmployees',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      formLabelWidth: '80px',
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      hireType: enumObj.hireType,
      treeShow: false,
      defaultProps: {
        label: 'name'
      },
      treeData: [],
      treeLoading: false,
      rules: {
        username: [
          {
            required: true, message: '请输入员工姓名', trigger: 'blur'
          }
        ],
        mobile: [
          {
            required: true, message: '请输入员工手机号', trigger: 'blur'
          },
          {
            pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'
          }
        ],
        timeOfEntry: [
          {
            required: true, message: '请选择员工入职时间', trigger: 'blur'
          }
        ],
        formOfEmployment: [
          {
            required: true, message: '请选择员工聘用形式', trigger: 'blur'
          }
        ],
        workNumber: [
          {
            required: true, message: '请输入员工工号', trigger: 'blur'
          }
        ],
        departmentName: [
          {
            required: true, message: '请选择员工所属部门', trigger: 'blur'
          }
        ],
        correctionTime: [
          {
            required: true, message: '请选择员工转正时间', trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async deptFocus() {
      this.treeShow = true
      this.treeLoading = true
      const result = await getDepts()
      this.treeData = arrayToTree(result.data.depts, '')
      this.treeLoading = false
    },
    deptBlur(event) {
      if (event.relatedTarget == null || event.relatedTarget.className.includes(!'el-tree-node')) {
        this.treeShow = false
      }
    },
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.treeShow = false
      this.$refs.form.clearValidate('departmentName')
    },
    save() {
      this.$refs.form.validate(async v => {
        if (v) {
          await addEmp(this.form)
          this.$emit('update:showDialog', false)
          const pageInfo = this.$parent.pageInfo
          pageInfo.total++
          this.$parent.pageInfo.page = Math.ceil(pageInfo.total / pageInfo.size)
          this.$parent.loadData()
        }
      })
    },
    close() {
      this.form = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
      this.treeShow = false
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  border-radius: 10px;
  .el-dialog__header {
    background: linear-gradient(to right, #3e6ef8, #5b8cff);
    border-radius: 10px 10px 0 0;
  }
  .el-dialog__body {
    .el-form {
      .el-form-item {
        .el-form-item__content {
          width: 80%;
          .el-tree {
            position: absolute;
            top: 44px;
            z-index: 1;
            width: 100%;
            height: 176px;
            border: 1px solid #d7dae2;
            border-radius: 3px;
            box-shadow: 0 -3em 3em rgba(0,0,0,0.1), 0.3em 0.3em 1em rgba(0,0,0,0.3);
            overflow-y: scroll;
          }
          .el-input,.el-select {
            width: 100%;
          }
        }
      }
    }
  }
}
</style>
